<template>
  <div>
    <el-button @click="show_dialog">新增</el-button>
    <el-table :data="hos.list" style="width: 100%">
      <el-table-column align="center" prop="name" label="医院名称">
      </el-table-column>
      <el-table-column align="center" prop="introduce" label="医院简介">
      </el-table-column>
      <el-table-column align="center" prop="address" label="医院地址">
      </el-table-column>
      <el-table-column align="center" prop="flags" label="标记">
      </el-table-column>
      <el-table-column align="center" prop="longitude" label="经度">
      </el-table-column>
      <el-table-column align="center" prop="latitude" label="纬度">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template v-slot="scope">
          <span style="margin-right: 5px" @click="delete_hos(scope.row.id)"
            >删除</span
          >
          <span @click="edit_hos(scope.row)">修改</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="obj.pageSize"
      :current-page="obj.page"
      @current-change="change"
      :total="hos.total"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialog_hos" width="70%">
      {{ form_hos }}
      <el-form :model="form_hos">
        <el-form-item label="医院名称" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.name"></el-input>
        </el-form-item>
        <el-form-item label="医院简介" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.introduce"></el-input>
        </el-form-item>
        <el-form-item label="省市区" :label-width="formLabelWidth_hos">
          <el-cascader
            v-model="form_hos.pca"
            :options="pca"
            :props="{ expandTrigger: 'hover', label: 'name', value: 'name' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.address"></el-input>
        </el-form-item>
        <el-form-item label="标记" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.flags"></el-input>
        </el-form-item>
        <el-form-item label="经度" :label-width="formLabelWidth_hos">
          <el-input disabled v-model="form_hos.longitude"></el-input>
        </el-form-item>
        <el-form-item label="纬度" :label-width="formLabelWidth_hos">
          <el-input disabled v-model="form_hos.latitude"></el-input>
        </el-form-item>
        <el-form-item label="选择经纬度" :label-width="formLabelWidth_hos">
          <el-button @click='show_map'>打开地图</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_hos = false">取 消</el-button>
        <el-button type="primary" @click="save_hos">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择地点" :visible.sync="dialog_map" width="70%">
      <div id='Map' style='height:400px;'></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialog_map = false">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import pca from "../../assets/pca.json";
export default {
  computed: {
    ...mapState("hos", ["hos"]),
  },
  data() {
    return {
      pca,
      dialog_map:false,
      obj: {
        page: 1,
        pageSize: 1000,
      },
      title: "新增医院",
      dialog_hos: false,
      form_hos: {},
      formLabelWidth_hos: "120px",
      map:{}
    };
  },
  created() {
    this.findHos(this.obj);
  },
  methods: {
    ...mapActions("hos", ["findHos", "saveHos"]),
    // 初始化地图
    initMap() {
      this.map = new AMap.Map("Map", {
          resizeEnable: true
      });
    },
    // 获取经纬度
    showInfoClick(e){
      this.form_hos.longitude = e.lnglat.getLng();
      this.form_hos.latitude = e.lnglat.getLat();
    },
    // 给地图绑定单击事件
    clickOn(){
        this.map.on('click', this.showInfoClick);
    },
    // 显示地图
    show_map(){
      this.dialog_map = true;
      setTimeout(()=>{
        this.initMap();
        this.clickOn();
      },1000)
    },
    // 显示模态框
    show_dialog() {
      this.dialog_hos = true;
    },
    // 删除医院
    delete_hos() {},
    edit_hos() {},
    change() {},
    // 保存医院
    save_hos() {
      this.form_hos.province = this.form_hos.pca[0];
      this.form_hos.city = this.form_hos.pca[1];
      this.form_hos.area = this.form_hos.pca[2];
      this.saveHos(this.form_hos).then((res)=>{
          this.dialog_hos = false;
          this.findHos(this.obj);
      })
    },
    handleChange() {},
  },
};
</script>